﻿using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Configuration;
using Mysqlx;
using Newtonsoft.Json;
using ServiceStack;
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Configuration;
using System.Linq;
using System.Linq.Expressions;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using System.Threading.Tasks;
using ZH.Infrastructure.Attr;
using ZH.Infrastructure.Dto.Request.Login;
using ZH.Infrastructure.Dto.Request.Tech;
using ZH.Infrastructure.Dto.Request.Yuyue;
using ZH.Infrastructure.Dto.Response;
using ZH.Infrastructure.Dto.Response.admin;
using ZH.Infrastructure.Dto.Response.Article;
using ZH.Infrastructure.Dto.Response.Yuyue;
using ZH.Infrastructure.EFCore.MySql;
using ZH.Infrastructure.IOC;
using ZH.Infrastructure.Redis;
using ZH.Infrastructure.Tools;
using ZH.Service.Interface;
using static Microsoft.EntityFrameworkCore.DbLoggerCategory;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace ZH.Service.Instance
{
    [Inject_]
    public class Article_Service : Base_Service,I_Article_Service
    {
        private readonly Mobile_IOC _mobile_IOC;
        private readonly IConfiguration _configuration;
        public Article_Service(Mobile_IOC mobile_IOC, IConfiguration configuration)
        {
            _mobile_IOC = mobile_IOC;
            _configuration = configuration;
        }

        public async Task<Api_Response_Dto> Get_article(int? page, int? limit, string? key)
        {
            var data = await _mobile_IOC._article_EFCore
                .QueryAll(out int total,
                  Convert.ToInt32(page), Convert.ToInt32(limit),
                  true,
                  o => o.AiId,
                    d => (string.IsNullOrEmpty(key) || d.AiTitle.Contains(key))
                  )
                .Select(d => new Article_Response_Dto
                {
                    aiId=d.AiId,
                    author = d.AiAuthor,
                    release_time = d.ReleaseTime.ToString(),
                    img=d.AiImg,
                    title = d.AiTitle,
                    body = d.AiBody,
                    browse = d.AiBrowse,
                    collect =d.AiCollect
                }).ToListAsync();
       return Result(1, "ok", new
            {
                total,
                 data
            });
        }

        public async Task<Api_Response_Dto> update_art(Article_Response_Dto dto)
        {
            var ls=await _mobile_IOC._article_EFCore.QueryAll(d=>d.AiId==dto.aiId).FirstOrDefaultAsync();
            ls.AiAuthor= dto.author;
            ls.AiImg = dto.img;
            ls.AiTitle = dto.title;
            ls.AiBody = dto.body;
            _mobile_IOC._article_EFCore.Update(ls);
            var result = _mobile_IOC._article_EFCore.SaveChanges();
            return Result(result, "修改成功");
        }


        public async Task<Api_Response_Dto> Add_art(Article_Response_Dto dto)
        {
          Article ls=new Article();
            ls.AiId = Config.GUID();
            ls.AiAuthor = dto.author;
            ls.AiImg = "/wz/WZ3.jpg";
            ls.AiTitle = dto.title;
            ls.ReleaseTime=DateTime.Now;
            ls.AiBrowse = 0;
            ls.AiCollect = 0;
            ls.AiBody = dto.body;
            _mobile_IOC._article_EFCore.Add(ls);
            var result = await _mobile_IOC._article_EFCore.SaveChangesAsync();
            return Result(result, "添加成功");
        }

        public async Task<Api_Response_Dto> delete_art(string id)
        {
            var ls = await _mobile_IOC._article_EFCore.QueryAll(d => d.AiId == id).FirstOrDefaultAsync();

            _mobile_IOC._article_EFCore.Delete(ls);
            var result = await _mobile_IOC._article_EFCore.SaveChangesAsync();

            return Result(result, "删除成功");

        }

    }
}

